/*
 * Copyright 2015 Soulwolf Ching
 * Copyright 2015 The Android Open Source Project for WebViewJavascriptBridge
 * 
 * Licens ed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.jiedh.app.meiweidk.web.webkit;

import android.graphics.Bitmap;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * author: EwenQin
 * since : 2016/10/23 下午12:59.
 */
public class WJBridgeX5WebViewClient extends WebViewClient {

    private static final String TAG = "WJBridgeWebViewClient";

    private WJBridgeProvider mProvider;

    private WebViewClient webViewClient;


    public WJBridgeX5WebViewClient(WJBridgeProvider provider){
        this.mProvider = provider;
    }
    public WJBridgeX5WebViewClient(WJBridgeProvider provider,WebViewClient otherWebViewCliwent){
        this.mProvider = provider;
        this.webViewClient=otherWebViewCliwent;
    }

    public WebViewClient getWebViewClient() {
        return webViewClient;
    }

    public void setWebViewClient(WebViewClient webViewClient) {
        this.webViewClient = webViewClient;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println("shouldOverrideUrlLoading======================="+url);
        if (this.webViewClient != null) {
            return  mProvider.shouldOverrideUrlLoading(mProvider.getLoader(), url)
                    || this.webViewClient.shouldOverrideUrlLoading(view, url)
                    || super.shouldOverrideUrlLoading(view, url);
        }else {
            return mProvider.shouldOverrideUrlLoading(mProvider.getLoader(), url)
                    || super.shouldOverrideUrlLoading(view, url);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        System.out.println("onPageFinished======================="+url);
        this.mProvider.onPageFinished();
        if (this.webViewClient != null) {
            this.webViewClient.onPageFinished(view, url);
        }
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (this.webViewClient != null) {
            this.webViewClient.onPageStarted(view, url, favicon);
        }
    }
    @Override
    public void onLoadResource(WebView view, String url) {
        if (this.webViewClient != null) {
            this.webViewClient.onLoadResource(view, url);
        }
    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        if (this.webViewClient != null) {
            this.webViewClient.onReceivedError(view, errorCode, description, failingUrl);
        }
    }
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

        handler.proceed();
    }
}
